
include('physicalentity');
include('physics.shapes');
include('graphics.animation');

// A space rock.
new_class(
  'Asteroid',
  function(x, y, r) {
    this.initRadius = r;
    this.radius = r;
    PhysicalEntity.call(this, x, y, 0, 0.000001);
    this.ticks = 0;
    this.dying = false;
    this.game = null;
  },
  {
    draw: function(d) {
      var context = d.getContext();
      var gradient = context.createRadialGradient(0,0,0, 0,0,this.radius);
      gradient.addColorStop(0, "gray");
      gradient.addColorStop(1, "#222222");

      context.beginPath();
      context.arc(0, 0, this.radius, 0, Math.PI * 2, false);
      context.closePath();
      context.fillStyle = gradient;
      context.fill();
      context.strokeStyle = "#666666";
      context.stroke();

      // Outline
      context.beginPath();
      context.arc(0, 0, this.radius, 0, Math.PI * 2, false);
      context.closePath();
      context.strokeStyle = "#666666";
      context.stroke();
    },
    shape: function() {
      return new physics.shapes.Circle(this.x, this.y, this.radius);
    },
    destroy: function(game) {
      if (this.dying) return;
      // Set a flag so our update can know to start dying.
      this.dying = true;
      var an = new graphics.AnimateScale(2, 0);
      an.register('didStop', bind(function() {
        this.parentNode.removeChild(this);
        game.deleteEntity(this);
      }, this));
      an.start(this);
    }
  },
  PhysicalEntity);
